package top.lyc829.mymusic.model.repository

import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import top.lyc829.mymusic.model.http.AppException
import top.lyc829.mymusic.model.http.MyNetwork
import top.lyc829.mymusic.util.Const
import java.lang.RuntimeException

/**
 * @Author lyc
 * @create 2021-07-18 00:03
 */
class SongRepository (private val network : MyNetwork) {

    suspend fun getSongs(ids: String) = withContext(Dispatchers.IO) {
        val result = network.getSongs(ids)
        if (result.code != Const.SUCCESS) {
            throw AppException(result.code, "获取歌单歌曲失败")
        }
        result.songs
    }

    suspend fun getSongsUrl(id: String) = withContext(Dispatchers.IO) {
        val result = network.getSongsUrl(id)
        Log.d(TAG, "songUrl:-----------$result")
        if (result.code != Const.SUCCESS) {
            throw AppException(result.code, "获取歌单歌曲失败")
        }
        result.data
    }



    companion object {
        private var instance: SongRepository? = null
        fun getInstance(network: MyNetwork): SongRepository {
            if (instance == null) {
                synchronized(SongRepository::class.java) {
                    if (instance == null) {
                        instance = SongRepository(network)
                    }
                }
            }
            return instance!!
        }

        const val TAG = "SongRepository"
    }
}